/*
  RCSwitch - Arduino libary for remote control outlet switches
  Copyright (c) 2011 Suat �zg�r.  All right reserved.

  Contributors:
  - Andre Koehler / info(at)tomate-online(dot)de
  - Gordeev Andrey Vladimirovich / gordeev(at)openpyro(dot)com
  - Skineffect / http://forum.ardumote.com/viewtopic.php?f=2&t=46
  - Dominik Fischer / dom_fischer(at)web(dot)de
  - Frank Oltmanns / <first name>.<last name>(at)gmail(dot)com
  
  Project home: http://code.google.com/p/rc-switch/

  This library is free software; you can redistribute it and/or
  modify it under the terms of the GNU Lesser General Public
  License as published by the Free Software Foundation; either
  version 2.1 of the License, or (at your option) any later version.

  This library is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  Lesser General Public License for more details.

  You should have received a copy of the GNU Lesser General Public
  License along with this library; if not, write to the Free Software
  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
*/
#ifndef _RCSwitch_h
#define _RCSwitch_h

#if defined(ARDUINO) && ARDUINO >= 100
    #include "Arduino.h"
#elif defined(ENERGIA) // LaunchPad, FraunchPad and StellarPad specific
    #include "Energia.h"	
#else
    #include "WProgram.h"
#endif


// At least for the ATTiny X4/X5, receiving has to be disabled due to
// missing libm depencies (udivmodhi4)
#if defined( __AVR_ATtinyX5__ ) or defined ( __AVR_ATtinyX4__ )
#define RCSwitchDisableReceiving
#endif

// Number of maximum High/Low changes per packet.
// We can handle up to (unsigned long) => 32 bit * 2 H/L changes per bit + 2 for sync
//#define RCSWITCH_MAX_CHANGES 67

// Nexa have 66 pulses + initial LOW
#define RCSWITCH_MAX_CHANGES 133

// Protocol 2: EV1527  RWSwitch claim pulse 354
// http://aitendo3.sakura.ne.jp/aitendo_data/product_img/wireless/315MHz-2012/RX315-HT48R/EV1527.pdf

#define PROTOCOL4_SYNC_FACTOR   71
#define PROTOCOL4_0_HIGH_CYCLES  4
#define PROTOCOL4_0_LOW_CYCLES  11
#define PROTOCOL4_1_HIGH_CYCLES  9
#define PROTOCOL4_1_LOW_CYCLES   6

// Protocol 5 is used by MC145026.
// 0  H_______H_______
// 1  HHHHHHH_HHHHHHH_
// F  HHHHHHH_H_______
// http://www.freescale.com/files/rf_if/doc/data_sheet/MC145026.pdf
#define PROTOCOL_MC145026 5
#define MC145026_PULSE   275    // RCSwitch claim 283-284... Maybe change tdef to 283 then??
#define MC145026_PULSES_PER_DIGIT 16
#define MC145026_SEPARATOR_DIGITS 3
#define MC145026_SHORT_CYCLES  1
#define MC145026_LONG_CYCLES  7
#define MC145026_DATAGRAM_SEPARATOR_CYCLES  (MC145026_SEPARATOR_DIGITS * MC145026_PULSES_PER_DIGIT)


#define PROTOCOL_NEXA 6
#define NEXA_PULSE   225 // 300   // eller er 224 mer rett? sjekk wav compare!! mellom 224 - 227
//#define NEXA_SYCNH_LOW   (NEXA_PULSE * 9)
//#define NEXA_DIGIT_SEPARATOR   (NEXA_PULSE * 3)  //1011. separator between pulses
//#define NEXA_DATA_SEPARATOR  (NEXA_PULSE * 34)    // pause between datagrams

class RCSwitch {

  public:
    RCSwitch();
    
    void switchOn(int nGroupNumber, int nSwitchNumber);
    void switchOff(int nGroupNumber, int nSwitchNumber);
    void switchOn(char* sGroup, int nSwitchNumber);
    void switchOff(char* sGroup, int nSwitchNumber);
    void switchOn(char sFamily, int nGroup, int nDevice);
    void switchOff(char sFamily, int nGroup, int nDevice);
    void switchOn(char* sGroup, char* sDevice);
    void switchOff(char* sGroup, char* sDevice);
    void switchOn(char sGroup, int nDevice);
    void switchOff(char sGroup, int nDevice);
  
    void send(unsigned long code, unsigned int length);
    void send(unsigned long long code, unsigned int length);

    void sendEx(char* code);

        
    #if not defined( RCSwitchDisableReceiving )
    void enableReceive(int interrupt);
    void enableReceive();
    void disableReceive();
    bool available();
    void resetAvailable();
	
    unsigned int getReceivedCodeLen();
    unsigned int getReceivedDelay();
    unsigned int getReceivedProtocol();
    unsigned int* getReceivedRawData();
    unsigned int getReceivedRawDataLen();
    char* getReceivedCode();
    char* getReceivedCodeDesc();
    #endif
  
    void enableTransmit(int nTransmitterPin);
    void disableTransmit();
    void setPulseLength(int nPulseLength);
    void setRepeatTransmit(int nRepeatTransmit);
    #if not defined( RCSwitchDisableReceiving )
    void setReceiveTolerance(int nPercent);
    #endif
    void setProtocol(int nProtocol);
    void setProtocol(int nProtocol, int nPulseLength);
  
  private:
    void sendProtocol1_01F(char* code);
    void sendProtocol1_01F_repeat(char* code);
    void sendProtocol2_01(char* code);        
    void sendProtocol3_01(char* code);
    void sendProtocol4_01(char* code);
    void sendProtocol5_01F(char* code);
    void sendProtocol6_012(char* code);
    void sendProtocol6Pulses(unsigned int shortPulses);
    
    void delayCycles(unsigned int cycles);
    
    char* getCodeWordB(int nGroupNumber, int nSwitchNumber, boolean bStatus);
    char* getCodeWordA(char* sGroup, int nSwitchNumber, boolean bStatus);
    char* getCodeWordA(char* sGroup, char* sDevice, boolean bStatus);
    char* getCodeWordC(char sFamily, int nGroup, int nDevice, boolean bStatus);
    char* getCodeWordD(char group, int nDevice, boolean bStatus);
   
    void transmit(int highPulses, int lowPulses);

    static char* dec2binWzerofill(unsigned long long dec, unsigned int length);
    static char* dec2binWcharfill(unsigned long dec, unsigned int length, char fill);
    
    #if not defined( RCSwitchDisableReceiving )
    static void handleInterrupt();
    static bool receiveProtocol1(unsigned int changeCount);
    static bool receiveProtocol2(unsigned int changeCount);
    static bool receiveProtocol3(unsigned int changeCount);
    static bool receiveProtocol4(unsigned int changeCount);    
    static bool receiveProtocol5(unsigned int changeCount);        
    static bool receiveProtocol6(unsigned int changeCount);        
    int nReceiverInterrupt;
    #endif
    int nTransmitterPin;
    int nPulseLength;
    int nRepeatTransmit;
    char nProtocol;



    #if not defined( RCSwitchDisableReceiving )
    static int nReceiveTolerance;
    static unsigned int nChangeCount;
    static unsigned int nReceivedCodeLen;
    static unsigned int nReceivedDelay;
    static unsigned int nReceivedProtocol;
    static char nReceivedCode[65];
    static char nReceivedCodeDesc[20];
    static bool bAvailable;

    /* 
     * timings[0] contains sync timing, followed by a number of bits
     */
    static unsigned int timings[RCSWITCH_MAX_CHANGES];
    #endif
    
};

#endif
